﻿
var InsertCalculatorLandTour = function (partnerElement, landTourElement, dateBeginElement, priceAdultElement, priceChildElement, quantityAdultElement, quantityChildElement, vatElement, totalElement, tourIdElement, priceBabyElement, quantityBabyElement, noteElement) {

    //ham tinh tong gia
    var fncTotalPriceLandTour = function () {
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby = $(priceBabyElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var vat = $(vatElement).val();
        var totalprice = 0;
        if (priceAdult > 0 && priceChild > 0) {
            totalprice = (priceAdult * quantityAdult) + (priceChild * quantityChild) + (priceBaby * quantityBaby);
            vat =  totalprice *0.1;
        }
        $(totalElement).text(totalprice);
    $(vatElement).val(vat);
    };
    //Load danh sách landtour
    var fncLoadLandTour = function (partner) {
        var partnerId = $(partnerElement).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "LandTourByPartner", "PartnerId": partnerId },
            dataType: "json",
            success: function (data) {
                $(landTourElement).html('<option value="-1">--- Chọn Land Tour ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(landTourElement)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }
                $(landTourElement).val(partner);
            }
        });
    };
    $(partnerElement).change(fncLoadLandTour);

    var fncLoadPriceLandTour = function () {
        var priceId = $(landTourElement).val();
        var dateBegin = $(dateBeginElement).val();
        $(priceChildElement).val('0');
        $(priceAdultElement).val('0');
        $(priceBabyElement).val('0');
        $('#btnInsertLandTour').attr('price', '-1');
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "PriceLandTour", "PriceId": priceId, "DateBegin": dateBegin },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultElement).val(data.PriceAdult);
                    $(priceChildElement).val(data.PriceChild);
                    $(priceBabyElement).val(data.PriceBaby);
                    $('#btnInsertLandTour').attr('price', data.PriceId);
                }
                fncTotalPriceLandTour();
            }
        });
    };
    $(landTourElement).change(fncLoadPriceLandTour);
    $(dateBeginElement).change(fncLoadPriceLandTour);
    
    $(quantityAdultElement).keyup(fncTotalPriceLandTour);
    $(quantityChildElement).keyup(fncTotalPriceLandTour);
    $(quantityBabyElement).keyup(fncTotalPriceLandTour);
    $(priceBabyElement).keyup(fncTotalPriceLandTour);
    
    //ham them sua xoa
    var fncLandTour = function() {
        var typeId = 12;
        var tourId = parseInt(tourIdElement);
        //var ticketId = $(this).attr('price');
        var ticketId = $(landTourElement).val();
        var dateBegin = $(dateBeginElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby = $(priceBabyElement).val();
        var vat = $(vatElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var note = $(noteElement).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "InsertLandTour", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "DateBegin": dateBegin, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "PriceAdult": priceAdult, "PriceChild": priceChild, "VAT": vat, "CalculatorId": calculatorId, "ClassName": className, "PriceBaby": priceBaby, "QuantityBaby": quantityBaby, "Note": note },
            dataType: "json",
            success: function(data) {

                $(".ListLandTour").remove();
                $(partnerElement).val('-1');
                $(dateBeginElement).val('');
                $(landTourElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(priceBabyElement).val('');
                $(quantityAdultElement).val('');
                $(quantityChildElement).val('');
                $(quantityBabyElement).val('');
                $(vatElement).val('');
                $(totalElement).text('');
                $(noteElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function(index, item) {
                        $('#new-ticket-LandTour')
                            .before($("<tr class='ListLandTour'></tr>").attr("id", 'LandTour-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.LandTourName + '</td>' +
                                '<td >' + item.BeginDate + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td>' + item.VAT + '</td>' +
                                '<td>' + item.Note + '</td>' +
                                '<td>' +
                                '<input class="btnUpdateLandTour" onclick="openPopup(\'ticket-landtour\')"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteLandTour"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalLandTourPrices').html(data.TotalLandTourPrices);
                    $('#TotalLandTourPricesAdults').html(data.TotalLandTourPricesAdults);
                    $('#TotalLandTourPricesChild').html(data.TotalLandTourPricesChilds);
                    $('#TotalLandTourPricesBaby').html(data.TotalLandTourPricesBabys);
                    $('#TotalVatLandTour').html(data.TotalVatLandTour);
                    $('#btnInsertLandTour').attr("calculatorid", "-1").val("Thêm");

                }

            }
        });
        hidePopup('ticket-landtour');
    };
    $('#btnInsertLandTour').on("click", fncLandTour);
    $('.btnDeleteLandTour').on("click", fncLandTour);
    $('.btnUpdateLandTour').on('click', function () {
        var calculatorId = $(this).attr("calculatorid");

        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "UpdateCalculatorLandTour", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#LandTour-' + calculatorId).remove();

                $(partnerElement).val(data.PartnerId);
                fncLoadLandTour(data.LandTourId);

                $(dateBeginElement).val(data.DateBegin);
                $(quantityAdultElement).val(data.QuantityAdult);
                $(quantityChildElement).val(data.QuantityChild);
                $(quantityBabyElement).val(data.QuantityBaby);
                $(priceAdultElement).val(data.PriceAdult);
                $(priceChildElement).val(data.PriceChild);
                $(priceBabyElement).val(data.PriceBaby);
                $(vatElement).val(data.Vat);
                $(totalElement).text(data.TotalPrice);
                $(noteElement).val(data.Note);


                $('#btnInsertLandTour').attr("calculatorid", calculatorId).attr('price', data.PriceId).val("Lưu");
                $('.btnUpdateLandTour').hide();
                $('#btnDeleteLandTour').show();
            }
        });
    });
}
    